#include <bits/stdc++.h>

using namespace std;

const int N = 50010;
int f[N][2];
class Solution
{
public:
    int maxProfit(vector<int> &prices, int fee)
    {
        memset(f, -0x3f, sizeof(f));
        for (int i = 0; i <= prices.size(); ++i)
        {
            f[i][0] = 0;
        }
        for (int i = 1; i <= prices.size(); ++i)
        {
            f[i][0] = max(f[i - 1][0], f[i - 1][1] + prices[i - 1] - fee);
            f[i][1] = max(f[i - 1][1], f[i - 1][0] - prices[i - 1]);
        }
        return f[prices.size()][0];
    }
};